

$(function () {
	$.get(member_coupon_view,function (res) {
		var data = JSON.parse(res);
		var couponId = getQueryString('id');
		if(data.status === 1) {
			var coupon = data.coupon;
            var html = '';
            for(var i = 0;i < coupon.length;i++ ){
            	if(couponId == coupon[i].id) {
            		console.log(coupon[i])
            		var start_time = formatDate(coupon[i].start_time);
            		var end_time = formatDate(coupon[i].end_time);
            		//var deadline = formatDateDay(coupon[i].deadline);//formatDate
					var deadline = formatDate(coupon[i].deadline);//formatDate
            		if(coupon[i].enable == '1') {
            			//var success = '<div class="success">领券成功</div>';
						var success = '';
						if(coupon[i].status == '1') {
							//var status = '状态已使用';
							$('.kq-btn').html('已核销');
							var status = '';
						}else{
							//var status = '状态可使用';
							var id=coupon[i].order_id;
							$('.kq-btn').html('<span onclick="kqbtn('+id+')">显示核销二维码</span>');
							var status = '';
						}
					}else{
            			var success = '';
            			$('.kq-btn').html('<span onclick="kqbtn()">免费领券</span>');
            		}
            		var term = escapeHtml(coupon[i].term);
            		var course = escapeHtml(coupon[i].course);
            		var class_time = escapeHtml(coupon[i].class_time);

            		var html = '';
            		var deschtml = '';
            		html = '<div class="kq-container-header">'+
								'<span>'+coupon[i].subtitle+'</span>'+
								'<h3>'+coupon[i].name+'</h3>'+
								'<div class="num-time">'+
									'<div class="num">'+
										'<span>报名数:</span>'+
										'<span>'+coupon[i].applicant+'/'+coupon[i].up_limit+'</span>'+
									'</div>'+
									'<div class="time">'+
										'<span>截止日期:</span>'+
										'<span>'+deadline+'</span>'+
									'</div>'+
								'</div>'+
							'</div>'+
							'<div class="kq-container-content">'+
								'<div class="content-topbar">'+
									'<div class="topbar">'+
										'<div>活动时间</div>'+
										'<span>'+class_time+'</span>'+
									'</div>'+
									'<div class="topbar">'+
										'<div>活动对象：</div>'+
										'<span>'+coupon[i].person+'</span>'+
									'</div>'+
								'</div>'+
								'<div class="tips">'+
									'<h3>使用须知</h3>'+
									'<span style="font-size:14px;">有效期:'+end_time+'</span>'+
									'<p>'+term+'</p>'+
									//'<i class="iconal-right" onclick="showTips(this)"></i>'+
									//'<i class="iconal-right"></i>'+
								'</div>'+
								'<div class="address">'+
									'<h3 class="address-title">活动地址:</h3>'+
									'<span>'+coupon[i].address+'</span>'+
								'</div>'+
								'<div class="description">'+
									'<div class="desc-header">'+
										'<h3>活动介绍：</h3>'+
										//'<i class="iconal-right" onclick="showTips(this)"></i>'+
										//'<i class="iconal-right"></i>'+
									'</div>'+
									'<div class="desc-content">'+
										'<p>'+course+'</p>'+
										'<div class="desc-image">'+
											'<img src='+coupon[i].pic+'>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+success;
					deschtml = '<div class="desc-container">'+
									'<div class="desc-header">'+
										'<h3>'+coupon[i].name+'</h3>'+
										'<span>'+status+'</span>'+
									'</div>'+
									'<div class="tiem-address">'+
										'<div class="time">'+
											'<div>活动时间</div>'+
											'<span>'+class_time+'</span>'+
										'</div>'+
										'<div class="address">'+
											'<div>地点</div>'+
											'<span>'+coupon[i].address+'</span>'+ 
										'</div>'+
									'</div>'+
									'<div class="rule">'+
										'<h3>使用规则：无需预约，直接至少年宫刷二维码即可</h3>'+
										'<div class="image">'+
						
										'</div>'+
									'</div>'+
								'</div>';

            	}
            }
            $('.kq-container').html(html);
            $('.kqdetail-desc').append(deschtml);
		}
	})
})

function formatDate (ns) {   //转换时间戳
    // return new Date(parseInt(ns) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
    var ns = new Date(parseInt(ns) * 1000);
       var Year  = ns.getFullYear();
       var Month = ns.getMonth()+1;
       var DateDay = ns.getDate();
       var Hour = ns.getHours();
       var Minute = ns.getMinutes().toString().length == 1? '0'+ns.getMinutes() : ns.getMinutes();
       return Year+'/'+Month+'/'+DateDay+' '+Hour+':'+Minute;        
}
function formatDateDay (ns) { //Y-M-D
	return new Date(parseInt(ns) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');
}

// 显示卡券详细信息
function kqbtn(id) {
	if(id) {
		$.get(detailkq, {op: 'code', id: id}, function (data) {
			var data = JSON.parse(data);
			if (data.status == 1) {
				$('#erweima img').attr('src', data.url);
				$('#erweima').fadeIn();
			}
		});
	}
	if($('.kq-btn span').text() == '免费领券'){
		$.get(member_blacklist_view,function (res) {
			var data = JSON.parse(res);
			if(data.status === 1) {
				if(!data.blacklist == '1'){
					//$('.kq-container').append('<div class="success">领券成功</div>');
					//$('.kq-btn').html('点击查看卡券');
					$.post(member_favorabler_add,
					{
						favorable_id: getQueryString('id')
					},function (res) {
							if(JSON.parse(res).status=='0'){
								$.toast('该券已领完','cancel');
							}else {
								$.toast('领取成功');
								setTimeout(function () {
									location.reload();
								}, 800);
							}
					})
				}else{
					$.toast('您被限制预约','text');
					return;
				}
			}
		})
	}else if($('.kq-btn').text() == '点击查看卡券'){
		$('.kqdetail-desc').fadeIn();
	}
}

$('.iconal-roundclosefill').click(function () {
	$('.kqdetail-desc').fadeOut();
});

// 显示Tips详情
function showTips(that) {
	var title = $(that).parent().find('h3').text();
	if($(that).parent().attr('class') == 'tips'){
		var text = $(that).parent().find('p').text();
		// text = escapeHtml(text);
	}else{
		var text = $(that).parent().parent().text(); 
		console.log(text);
	}
	$('.tips-container h3').html(title);
	$('.tips-container p').html(text);
	$('.tips-desc').fadeIn();
}

$('.tips-desc').click(function (event) {
	event.stopPropagation(); 	
	$(this).fadeOut();
});

function escapeHtml(text) {
	var a=text.replace(/(\&lt;)/g,"<");
	var b=a.replace(/(\&gt;)/g,">");
	var c=b.replace(/(\&amp;)/g,"\&");
	var d=c.replace(/(\&quot;)/g,"\"");
	return d;
}
